package com.zyd.shiro.business.controller.pak;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Assets;
import com.zyd.shiro.business.entity.Distribute;
import com.zyd.shiro.business.entity.Warehousing;
import com.zyd.shiro.business.service.PakAssetsService;
import com.zyd.shiro.business.service.PakDistributeLineService;
import com.zyd.shiro.business.service.PakDistributeService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.PakDistributeLine;
import com.zyd.shiro.persistence.beans.PakPurchaseLine;
import com.zyd.shiro.persistence.beans.PakWarehousing;
import com.zyd.shiro.util.ResultUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zyd.shiro.persistence.beans.PakDistribute;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.io.File;
import com.zyd.shiro.utils.FileUtils;
import com.zyd.shiro.utils.ExcelUtil;
import java.util.Date;


@RestController
@RequestMapping("/distribute")
@Api(description = "资产派发")
public class PakDistributeController {
    @Autowired
    private PakDistributeService distributeService;



    @Autowired
    private PakAssetsService assetsService;

    @PostMapping
    @ApiOperation(value="添加", produces="application/json")
    public ResponseVO insert(Distribute distribute) {
        distributeService.insert(distribute);
        return ResultUtil.success("添加成功！",null);
    }

    @PutMapping
    @ApiOperation(value="修改", produces="application/json")
    public ResponseVO update(Distribute distribute) {
        distributeService.update(distribute);
        return ResultUtil.success("修改成功！",null);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value="主键删除", produces="application/json")
    public ResponseVO delete(@PathVariable(name="id") Long id){
        distributeService.removeByPrimaryKey(id);
        return ResultUtil.success("删除成功！",null);
    }

    @GetMapping
    @ApiOperation(value="分页查询", produces="application/json")
    public PageResult page(BaseConditionVO vo) {
        PageInfo<Distribute> pageInfo = distributeService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value="查询所有", produces="application/json")
    public ResponseVO list() {
        List<Distribute> distributes = distributeService.listAll();
        return ResultUtil.success("查询成功！",distributes);
    }

  /*  @PostMapping(value = "/import")
    @ApiOperation(value="批量导入", produces="application/json")
    public ResponseVO folderImport(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        if (suffix.equals(".xlsx") || suffix.equals(".xls")) {
            File newFile = new File(FileUtils.createFilePath(fileName));
            file.transferTo(newFile);
            ExcelUtil<PakDistribute> excelUtil = new ExcelUtil<>();
            List<PakDistribute> acfForms = excelUtil.importExcel("Sheet1", newFile, PakDistribute.class);
            for (PakDistribute a : acfForms) {
                a.setCreateTime(new Date());
                a.setUpdateTime(new Date());
                distributeService.increase(a);
            }
            return ResultUtil.success("修改成功！");
        }
        return ResultUtil.error("请输入excel文件!");
    }*/

    @PutMapping(value = "/distribute/{id}")
    @ApiOperation(value="派发", produces="application/json")
    public ResponseVO distribute(@PathVariable(name="id") Long id) {
        Distribute distribute = distributeService.getByPrimaryKey(id);
        PakDistribute pakDistribute = distribute.getPakDistribute();
        if(pakDistribute.getAppstatus()!=1){
            return ResultUtil.error("未通过审批！");
        }
        if(pakDistribute.getDisstatus()!=0){
            return ResultUtil.error("派发已处理！");
        }
        pakDistribute.setDisstatus(1);
        distributeService.updateSelective(distribute);
        return ResultUtil.success("派发成功！",distribute);

    }
    @PutMapping(value = "/receive/{id}")
    @ApiOperation(value="领取", produces="application/json")
    public ResponseVO receive(@PathVariable(name="id") Long id) {
        Distribute distribute = distributeService.getByPrimaryKey(id);
        PakDistribute pakDistribute = distribute.getPakDistribute();
        if(pakDistribute.getAppstatus()!=1){
            return ResultUtil.error("未通过审批！");
        }
        if(pakDistribute.getDisstatus()!=1){
            return ResultUtil.error("还未派发或已派发！");
        }
        if(pakDistribute.getClaimstatus()!=0){
            return ResultUtil.error("已领取！");
        }
        assetsService.distribute(pakDistribute);
        pakDistribute.setClaimstatus(1);
        distributeService.updateSelective(distribute);
        return ResultUtil.success("领取成功！",distribute);
    }
    @PutMapping(value = "/approve/{id}")
    @ApiOperation(value="审批", produces="application/json")
    public ResponseVO approve(@PathVariable(name="id") Long id,int status) {
        Distribute distribute = distributeService.getByPrimaryKey(id);
        distribute.getPakDistribute().setAppstatus(status);
        distributeService.updateSelective(distribute);
        return ResultUtil.success("审批成功！",null);
    }


}